package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.domain.Role;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.RoleMapper;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    //注入Mapper
    @Autowired
    private RoleMapper roleMapper;

    //添加角色
    public void add(Role role,Integer[] permissionIds,Integer[] menuIds) {
        roleMapper.add(role);
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);
    }

    //分页查询
    public PageResult findPage(QueryPageBean queryPageBean,String username) {
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //获取
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Map<String,String> map = new HashMap<String,String>();
        map.put("username",username);
        map.put("value",queryString);
        Page<Role> page = roleMapper.selectByCondition(map);
        long total = page.getTotal();
        List<Role> result = page.getResult();
        return new PageResult(total, result);
    }

    //根据id查询
    public Role findById(Integer id) {
        return roleMapper.findById(id);
    }

    //通过角色id查询对应的权限ids
    public List<Integer> findPermissionIdsByRoleId(Integer id) {
        List<Integer> list = roleMapper.findPermissionIdsByRoleId(id);
        return list;
    }

    //通过角色id查询对应的菜单ids
    public List<Integer> findMenuIdsByRoleId(Integer id) {
        List<Integer> list = roleMapper.findMenuIdsByRoleId(id);
        return list;
    }

    //根据id删除
    public void delete(Integer id) {
        //先删除关联关系
        roleMapper.deletePermissionIds(id);
        roleMapper.deleteMenuIds(id);
        roleMapper.deleteUserIds(id);
        //删除角色
        roleMapper.delete(id);
    }

    //修改
    public void edit(Role role,Integer[] permissionIds,Integer[] menuIds) {
        //1.修改角色
        roleMapper.edit(role);
        //2.删除原有的关联关系
        roleMapper.deletePermissionIds(role.getId());
        roleMapper.deleteMenuIds(role.getId());
        //3.重新添加修改后的关联关系
        setRoleAndPermission(role.getId(),permissionIds);
        setRoleAndMenu(role.getId(),menuIds);
    }

    //查询所有
    public List<Role> findAll() {
        return roleMapper.findAll();
    }

    //根据角色名查询该角色是否存在
    public Role findByName(String name) {
        return roleMapper.findByName(name);
    }

    //根据角色关键字查询数据库是否该角色已存在
    public Role findByKeyword(String keyword) {
        return roleMapper.findByKeyword(keyword);
    }

    //根据角色id查询关联数据的数量
    public Integer findCountByRoleId(Integer roleId) {
        //查询跟该角色关联的所有权限数量
        Integer count1 = roleMapper.findPermissionCountByRoleId(roleId);
        //查询跟该角色关联的所有菜单数量
        Integer count2 = roleMapper.findMenuCountByRoleId(roleId);
        //查询跟该角色关联的所有用户数量
        Integer count3 = roleMapper.findUserCountByRoleId(roleId);
        return count1 + count2 + count3;
    }

    //查询所有角色排除自己的角色
    public List<Role> findAllExOwn(Integer id) {
        return roleMapper.findAllExOwn(id);
    }

    //设置角色和权限的关联关系
    public void setRoleAndPermission(Integer roleId,Integer[] permissionIds){
        //判断permissionIds是否为空
        if(permissionIds != null && permissionIds.length > 0){
            //不为空就进行遍历
            for (Integer permissionId : permissionIds) {
                //创建map容器,存储关联关系
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                //项中间表插入数据
                roleMapper.setRoleAndPermission(map);
            }
        }
    }

    //设置角色和菜单的关联关系
    public void setRoleAndMenu(Integer roleId,Integer[] menuIds){
        //判断menuIds是否为空
        if(menuIds != null && menuIds.length > 0){
            //不为空就进行遍历
            for (Integer menuId : menuIds) {
                //创建map容器,存储关联关系
                Map<String,Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuId",menuId);
                //项中间表插入数据
                roleMapper.setRoleAndMenu(map);
            }
        }
    }
}
